package com.liuzh.kaoyanspringboot.service.impl;

import com.liuzh.kaoyanspringboot.entity.RoleMenu;
import com.liuzh.kaoyanspringboot.entity.Role;
import com.liuzh.kaoyanspringboot.mapper.RoleMapper;
import com.liuzh.kaoyanspringboot.mapper.RoleMenuMapper;
import com.liuzh.kaoyanspringboot.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.beans.Transient;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 刘志鸿
 * @since 2023-03-03
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Resource
    RoleMenuMapper roleMenuMapper;

    @Transient
    @Override
    public void setRoleMenu(Integer roleId, List<Integer> menuIds) {
        // 先删除所有的绑定关系
        roleMenuMapper.deleteByRoleId(roleId);
        //再添加 前端传递的 menuIds
        for (Integer menuId : menuIds) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(menuId);
            roleMenuMapper.insert(roleMenu);
        }
    }

    @Override
    public List<Integer> getRoleMenu(Integer roleId) {
        return roleMenuMapper.selectByRoleId(roleId);
    }

    @Override
    public boolean deleteById(Integer roleId) {
        // 先删除所有的绑定关系
        roleMenuMapper.deleteByRoleId(roleId);
        return removeById(roleId);
    }


}
